package com.netease.xone.adapter;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.netease.xone.R;
import com.netease.xone.widget.ViewPagerChild.Emoticon;


public class AdapterGridEmoticon extends BaseAdapter {
    
    private int px,py;
    private int mPadding;
    private Context mContext;
    
    /**
     * 存放表情资源
     */
    private List<Emoticon> mFreData = null;
    
    public AdapterGridEmoticon(Context context, List<Emoticon> list) {
        this.mContext = context;
        this.mFreData = list;
        mPadding = context.getResources().getDimensionPixelSize(R.dimen.discuss_size_10);
        px = context.getResources().getDimensionPixelSize(R.dimen.discuss_size_56);
        py = px;
    }

    @Override
    public int getCount() {
        if (null != mFreData) {
            return mFreData.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (null != mFreData) {
            return mFreData.get(position);
        }
        
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        
        if (convertView == null) {
            ImageView i = new ImageView(mContext);
            i.setPadding(mPadding, mPadding, mPadding, mPadding);
            i.setScaleType(ImageView.ScaleType.CENTER);
            i.setLayoutParams(new GridView.LayoutParams(px, py));
            convertView = i;
        }

        ImageView iv = null;
        if (convertView instanceof ImageView)
            iv = (ImageView) convertView;

        Emoticon phraseInfo = (Emoticon)getItem(position);
        if (phraseInfo != null) {
            if (iv != null) {
                iv.setImageResource(phraseInfo.key);
                iv.setTag(phraseInfo.value);
            }
        }

        return iv;
    }
}
